/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

/**
 *
 * @author dimartinomirko
 */
public class ExecutionQuery {
    
    public static void executeQueryTest(Query query) {
        QueryExecution qe = QueryExecutionFactory.create(query);
        
        
        try {
            ResultSet rs = qe.execSelect();
            if ( rs.hasNext() ) {
                
                
                
                // show the result, more can be done here
                System.out.println(ResultSetFormatter.asText(rs));
                
                
            }
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            qe.close();
        }
        qe = QueryExecutionFactory.create(query);
        
        try {
            ResultSet rs = qe.execSelect();
            
            int count=0;
            while (rs.hasNext()) {
                rs.next();
                count++;
            }
            System.out.println("Number of results: " + count); 
            
        }
        
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            qe.close();
        }
    }
    
    
    public static void executeQueryTestEndpoint(Query query, String endpoint) {
        
        QueryExecution qe = QueryExecutionFactory.sparqlService(endpoint,query);
        
        
        try {
            ResultSet rs = qe.execSelect();
            if ( rs.hasNext() ) {
                
                
                
                // show the result, more can be done here
                System.out.println(ResultSetFormatter.asText(rs));
                
                
            }
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            qe.close();
        }
        qe = QueryExecutionFactory.sparqlService(endpoint,query);
        
        try {
            ResultSet rs = qe.execSelect();
            
            int count=0;
            while (rs.hasNext()) {
                rs.next();
                count++;
            }
            System.out.println("Number of results: " + count);
            System.out.println("");
            System.out.println("--------------------------------------------");
            
        }
        
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            qe.close();
        }
    }
    
    public static void executeQueryTestEndpointNoPrint(Query query, String endpoint) {
        
        QueryExecution qe = QueryExecutionFactory.sparqlService(endpoint,query);
        
        
        try {
            ResultSet rs = qe.execSelect();
            if ( rs.hasNext() ) {
                
                
                
                // show the result, more can be done here
                //System.out.println(ResultSetFormatter.asText(rs));
                
                
            }
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            qe.close();
        }
        
    }

    public static void executeQueryOverOriginalSources(Query query) {
        
        //Exexute query over DBpedia
        System.out.println("Results original query over DBpedia: ");
        executeQueryTestEndpoint(query, "http://dbpedia.org/sparql");
        
        //Exexute query over LinkedMDB
        System.out.println("Results original query over LinkedMovieDB: ");
        executeQueryTestEndpoint(query, "http://data.linkedmdb.org/sparql");
        
        
        //Exexute query over FactForge
        System.out.println("Results original query over FactForge: ");
        executeQueryTestEndpoint(query, "http://factforge.net/sparql");
        
        
        
    }
    
    public static void executeQueryTestNoPrint(Query query) {
        QueryExecution qe = QueryExecutionFactory.create(query);
        
        
        try {
            ResultSet rs = qe.execSelect();
            if ( rs.hasNext() ) {
                
                
                
                // show the result, more can be done here
                //System.out.println(ResultSetFormatter.asText(rs));
                
                
            }
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            qe.close();
        }
        
        
        
    }
    
}
